﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ElevatorLib
{
    /// <summary>
    /// Gamma分布
    /// </summary>
    public class GammaDistr : IDistribution
    {

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="rnd"></param>
        /// <param name="alpha"></param>
        /// <param name="beta"></param>
        public GammaDistr(Random rnd, int alpha,double beta)
        {
            if(alpha <= 0 || beta <= 0.0)
            {
                alpha = 0;
                beta = 0;
                throw new Exception("Alpha and beta must be greater than 0,0");
            }
            this.rnd = rnd;
            this.alpha = alpha;
            this.beta = beta;
        }

        #region Properties
        /// <summary>
        /// 随机数
        /// </summary>
        private Random rnd;
        /// <summary>
        /// 大于0
        /// </summary>
        private int alpha;
        /// <summary>
        /// 大于0
        /// </summary>
        private double beta;
        #endregion

        #region IDistribution 成员
        /// <summary>
        /// 生成数据
        /// </summary>
        /// <returns></returns>
        public double Sample()
        {
            double sum = 0.0;
            for (int i = 0; i < alpha; i++)
            {
                sum += Math.Log(Math.E, rnd.NextDouble());
            }

            return -beta * sum;
        }

        #endregion
    }
}
